import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

/* Layout */
import Layout from "@/layout";

// 4.过滤路由 静态路由不需要过滤
// 动态路由需要权限过滤
export const routes = [
  {
    path: "/login",
    component: () => import("@/views/Login/index"),
    hidden: true,
  },
  {
    path: "/",
    component: Layout,
    redirect: "/workbench",
  },
  {
    path: "/workbench",
    component: Layout,
    children: [
      {
        // 二级路由没路径以一级路由路径为准
        path: "",
        component: () => import("@/views/Workbench/index"),
        meta: { title: "工作台", icon: "el-icon-data-board" },
      },
    ],
  },
  {
    path: "/404",
    component: () => import("@/views/404"),
    hidden: true,
  },
  {
    path: "/car/addMonth",
    component: () => import("@/views/Car/CarCard/addMonthCard"),
  },
  {
    path: "/addEnterprise",
    component: () => import("@/views/Park/Enterprise/addEnterprise.vue"),
  },
  {
    // 配置动态路由
    path: "/enterpriseDetail/:id",
    component: () => import("@/views/Park/Enterprise/enterpriseDetail"),
  },
  {
    path: "/sys/addRole",
    component: () => import("@/views/System/Role/AddRole"),
  },
];

// 创建router实例
const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    mode: "history", //hash模式使用#链接，history使用/链接

    // 返回进入页面时滚动到指定位置
    scrollBehavior: () => ({ y: 0 }),
    routes: routes, //使用路由规则
  });

// 接收createRouter()的返回值
const router = createRouter();

// 重置路由方法
export function resetRouter() {
  // 得到一个全新的router实例对象
  const newRouter = createRouter();
  // 使用新的路由记录覆盖掉老的路由记录，迭代一下
  router.matcher = newRouter.matcher;
}

export default router;
